﻿using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SingleBlog.Core.Data
{
    [Table("Articles")]
    public class Article
    {
        /// <summary>
        /// 自增Id。
        /// </summary>
        [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 标题。
        /// </summary>
        [Display(Name ="标题")]
        [Required(ErrorMessage ="{0}是必填项"),StringLength(60,ErrorMessage ="{0}不能超过{1}个字")]
        public string Title { get; set; }
        /// <summary>
        /// 内容。
        /// </summary>
        [Display(Name ="正文")]
        [Required(ErrorMessage ="{0}是必填项")]
        public string Content { get; set; }
        /// <summary>
        /// 创建时间。
        /// </summary>
        public DateTime CreateTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 分类Id
        /// </summary>
        public int CategoryId { get; set; }
        /// <summary>
        /// 阅读量。
        /// </summary>
        [Display(Name ="阅读量")]
        [Range(0,int.MaxValue,ErrorMessage ="{0}不能小于0")]
        public int ViewCount { get; set; }

        /// <summary>
        /// 是否推荐文章
        /// </summary>
        [Display(Name = "推荐")]        
        public bool IsRecommand { get; set; }
        /// <summary>
        /// 自定义排序顺序，从小到大排序。
        /// </summary>
        [Display(Name = "排序", Description ="从小到大")]
        public int DisplayOrder { get; set; }

        /// <summary>
        /// Gets or sets the user identifier.
        /// </summary>
        /// <value>
        /// The user identifier.
        /// </value>
        public int UserId { get; set; }
        
        /// <summary>
        /// 增加阅读量。
        /// </summary>
        /// <param name="step"></param>
        public void IncreseView(int step = 1) => this.ViewCount += step;
        /// <summary>
        /// 设置推荐
        /// </summary>
        public void Recommand() => this.IsRecommand = true;
        /// <summary>
        /// 设置不推荐
        /// </summary>
        public void Unrecommand() => this.IsRecommand = false;

        #region 导航属性
        /// <summary>
        /// 关联分类。
        /// </summary>
        public virtual Category Category { get; set; }

        /// <summary>
        /// 关联用户。
        /// </summary>
        public virtual SingleBlogUser User { get; set; }

        /// <summary>
        /// 关联回复。
        /// </summary>
        public virtual ICollection<Comment> Comments { get; set; } = new HashSet<Comment>();
        #endregion

    }
}
